<?php
class controller
{
	protected $parts;
	public $params;
	
	public function __construct($urlString)
	{
		$urlString = strtolower($urlString);
		
		// $urlString: /类/方法[/参数1,参数2...参数n]
		
		// 去掉未尾的 '/'
		if(substr($urlString, -1, 1) == '/'){
			$urlString = substr($urlString, 0, strlen($urlString)-1);
		}
		
		$parts = explode('/', $urlString);
		
		if(empty($parts[0])){
			$parts[0] = 'index';
		}
		if(empty($parts[1])){
			$parts[1] = 'defaultaction';
		}
		$this->parts = $parts;
		$this->sectionaction = $parts[0] . '/' . $parts[1];
		array_shift($parts); array_shift($parts);
		$this->params = $parts;
	}
	
	public function render()
	{
		try{
			if(!class_exists($this->parts[0])){
				throw new SectionDoesntExistException("{$this->parts[0]} ".
					lang::NOT_A_VALID_MODULE);
			}
			
			if(!method_exists($this->parts[0], $this->parts[1])){
				throw new ActionDoesntExistException("{$this->parts[0]}/{$this->parts[1]} ".
					lang::NOT_A_VALID_ACTION);
			}
			
			$called = call_user_func_array(array(new $this->parts[0], $this->parts[1]), array($this->parts));
			
			if($called === FALSE){
				throw new ActionDoesntExistException("{$this->parts[0]}/{$this->parts[1]} ".
					lang::FAILED_TO_EXECUTE);
			}
		}catch(Exception $e){
			die($e->getMessage());
		}
	}
}